package com.fjzn.detect.core.result;

import com.fjzn.detect.common.enums.other.YesNo;
import com.fjzn.detect.common.enums.web.ResponseStatus;
import lombok.Data;

/**
 * 响应结果的封装
 *
 * @author yzh
 * @since 2021年04月25日15:33:58
 *
 */
@Data
public class ResponseResult<T> {

    /** 状态码 */
    private int code;
    /** 是否加密 */
    private YesNo encode = YesNo.NO;
    /** 返回内容 */
    private String msg;
    /** 数据对象 */
    private T data;

    private ResponseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private ResponseResult(int code, String desc) {
        this.code = code;
        this.msg = desc;
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static <T> ResponseResult<T> success()
    {
        return ResponseResult.success(null);
    }

    /**
     * 返回成功消息
     *
     * @param t 数据对象
     * @return 成功消息
     */
    public static <T> ResponseResult<T> success(T t)
    {
        return new ResponseResult<T>(ResponseStatus.操作成功.code(), ResponseStatus.操作成功.desc(), t);
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static <T>ResponseResult<T> error()
    {
        return ResponseResult.error(ResponseStatus.系统异常.code(), ResponseStatus.系统异常.desc());
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static <T>ResponseResult<T> error(String msg)
    {
        return ResponseResult.error(ResponseStatus.系统异常.code(),msg);
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static <T>ResponseResult<T> error(ResponseStatus status)
    {
        return ResponseResult.error(status.code(),status.desc());
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static <T>ResponseResult<T> error(int code,String msg)
    {
        return new ResponseResult<T>(code,msg);

    }
}
